﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;
using System.Xml;

namespace GTS.Channel.Api {
    /// <summary>
    /// 有乐通SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class YouLeTong : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "YouLeTong";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {

            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.IsHidden = false;
            info.Price = 10;
            info.DeviceCode = "GENERICDEVICE";

            //接收值
            //<?xml version="1.0" encoding="GBK"?>
            //<message>
            //<spid>cmcc-2000</spid>
            //<spcode>10662000</spcode>
            //<toicp></toicp>
            //<node>global</node>
            //<feecode>TPNKDJ</feecode>
            //<feeprice>100</feeprice>
            //<feetype>ITEM</feetype>
            //<product>YOULE</product>
            //<feecategory>null</feecategory>
            //<channel>PHONE_YXKJ_YXKJ038</channel>
            //<mobile>18238066394</mobile>
            //<linkid>17345005638196302691</linkid>
            //<simulate>false</simulate>
            //<format>15</format>
            //<content>aQ</content>
            //<isprovision>false</isprovision>
            //<issubscribe>false</issubscribe>
            //<createDate>2012-04-16 17:34:55</createDate>
            //</message>
            string result = Request2.GetRequestInputStream(Encoding.GetEncoding("GBK"));
            info.Mobile = result.GetMatchingValues("<mobile>(.+?)</mobile>", "<mobile>", "</mobile>").FirstOrDefault() ?? "";
            info.LongPhone = result.GetMatchingValues("<spcode>(.+?)</spcode>", "<spcode>", "</spcode>").FirstOrDefault() ?? "";
            info.LinkID = (result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "");
            info.ReceiveQuery = result.GetMatchingValues("<content>(.+?)</content>", "<content>", "</content>").FirstOrDefault() ?? "";;
            info.Price = ((result.GetMatchingValues("<feeprice>(.+?)</feeprice>", "<feeprice>", "</feeprice>").FirstOrDefault() ?? "").ToInt(100) / 10).IfLessThan(10, 10);
            info.RequestDate = (result.GetMatchingValues("<createDate>(.+?)</createDate>", "<createDate>", "</createDate>").FirstOrDefault() ?? "").ToDateTime();

            //XmlDocument xml = new XmlDocument();
            //xml.LoadXml(result);
            //XmlNodeList Parentlists = xml.SelectNodes("/message");
            //foreach (XmlNode temp1 in Parentlists) { //短信循环
            //    foreach (XmlNode temp in temp1.ChildNodes) { //一条短信信息
            //        if (temp.Name == "mobile") {
            //            info.Mobile = temp.InnerText;
            //        } else if (temp.Name == "toicp") { //长号码 
            //            info.LongPhone = temp.InnerText;
            //        } else if (temp.Name == "linkid") { //linkid
            //            info.LinkID = temp.InnerText + DateTime.Now.ToString("yyyyMMdd");
            //        } else if (temp.Name == "content") { //内容
            //            info.ReceiveQuery = temp.InnerText;
            //        } else if (temp.Name == "feeprice") { //价格
            //            info.Price = (temp.InnerText.ToInt(10) / 10).IfLessThan(10, 10);
            //        }
            //    }
            //}

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            string[] infos = info.ReceiveQuery.Split('_');
            if (infos.Length >= 4) {
                info.MountCode = infos[1].ToInt();
                info.GameCode = infos[2].PadLeft(5, '0');
                info.ChargeCode = infos[3];
            } else {
                info.MountCode = 493;
                info.GameCode = "00999";
                info.ChargeCode = "9";
            }

            //如果数据库中存在价格以数据库里为主
            ChannelInfo extInfo = Base.GetChannelInfo(SPCode, info.LongPhone, info.ReceiveQuery);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }

            return info;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            return null;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;

            //接收值
            string result = Request2.GetRequestInputStream(Encoding.GetEncoding("GBK"));
            info.LinkID = (result.GetMatchingValues("<linkid>(.+?)</linkid>", "<linkid>", "</linkid>").FirstOrDefault() ?? "");
            info.ResponseStatus = (result.GetMatchingValues("<statestr>(.+?)</statestr>", "<statestr>", "</statestr>").FirstOrDefault() ?? "") == "DELIVRD" ? 1 : 0;
            //info.MRDate = DateTime.Now;
            info.MRDate = (result.GetMatchingValues("<createDate>(.+?)</createDate>", "<createDate>", "</createDate>").FirstOrDefault() ?? "").ToDateTime();

            if (info.LinkID.IsNullEmpty()) return null;

            return info;
        }
        /// <summary>
        /// IVR的参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            //初始值
            IVRDealData info = new IVRDealData();
            info.SPCode = SPCode;
            info.SMSKey = 1;
            info.ResponseStatus = 1;

            //接收值
            info.Mobile = Request2.GetQ("mobile").Trim();
            info.FeeTime = Request2.GetQInt("feetime", 0);
            info.LinkID = Rand.RndDateStr() + Rand.RndNum(5);
            info.LongPhone = Request2.GetQ("spcode").Trim();
            string starttime = Request2.GetQ("start_time").Trim();
            string endtime = Request2.GetQ("end_time").Trim();
            if (starttime.Length == 14) info.BeginTime = DateTime.ParseExact(starttime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
            if (endtime.Length == 14) info.EndTime = DateTime.ParseExact(endtime, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            //如果数据库中存在价格以数据库里为主
            IVRChannelInfo extInfo = Base.GetIVRChannelInfo(SPCode, info.LongPhone);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }
            info.Price = Request2.GetQInt("feeprice", 0) / 10;

            return info;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res == ResultEnum.Success ? "0" : "1");
        }
    }
}

